package com.jz.cash.client.api;

import com.jz.cash.client.caches.UserCache;
import com.jz.cash.client.model.User;
import com.jz.cash.client.service.MenuConfigService;
import com.jz.cash.client.vo.Json;
import com.jz.cash.client.vo.MenuVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuConfigService menuConfigService;

    @GetMapping("getMenuList")
    public Json findMenuListByUserId() {

        User user  = (User)SecurityUtils.getSubject().getPrincipal();

        String oper = "GET Menu List";
        Set<MenuVo> menuList = menuConfigService.findMenuListByUserId(user.getId());

        //get User All Funcs
        Set<String> funcs = user.getPerms();
        Set<MenuVo> newList = new HashSet<>();
        if (!CollectionUtils.isEmpty(funcs)) {
             newList = menuList.stream().filter(f -> funcs.contains(f.getName())).collect(Collectors.toSet());
        }
        return Json.succ(oper).data("menuList", newList);
    }


}
